﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using WebVideo;

namespace WebVideo.Areas.Admin.Models
{
    public class SiteInfoModel
    {
        public int Id { get; set; }

        [Required]
        public string Title { get; set; }

        [Required]
        public string Contents { get; set; }

    
        public string Image { get; set; }

     
       // [RegularExpression("(https?://(www\\.)?youtube\\.com/.*v=\\w+.*)|(https?://youtu\\.be/\\w+.*)|(.*src=.https?://(www\\.)?youtube\\.com/v/\\w+.*)|(.*src=.https?://(www\\.)?youtube\\.com/embed/\\w+.*)", ErrorMessage = "You must enter URL youtube")]
        public string Video { get; set; }

        public DateTime? Date { get; set; }

        public static IEnumerable<EntitySiteInfo> Index()
        {

            dbVideoDataContext db = new dbVideoDataContext();
            var query = from p in db.EntitySiteInfos
                        select p;
            return query.ToList();
        }
        public static void Insert(SiteInfoModel Mtnew)
        {
            dbVideoDataContext db = new dbVideoDataContext();
            EntitySiteInfo tnew = new EntitySiteInfo();

            if (Mtnew.Video != null)
            {
                string[] arr = Mtnew.Video.ToString().Split('=');
                tnew.Video = arr[1].ToString();
            }

            tnew.Contents = Mtnew.Contents;
            tnew.Image = Mtnew.Image;
            tnew.Title = Mtnew.Title;
           
            tnew.Date = DateTime.Now;

            db.EntitySiteInfos.InsertOnSubmit(tnew);
            db.SubmitChanges();

        }


        public static SiteInfoModel LayModel(int Id)
        {
            SiteInfoModel Mtnew = new SiteInfoModel();
            
            dbVideoDataContext db = new dbVideoDataContext();

            EntitySiteInfo tnew = db.EntitySiteInfos.Where(p => p.Id == Id).Single();

            Mtnew.Id = tnew.Id;
            Mtnew.Title  = tnew.Title;
            Mtnew.Contents = tnew.Contents;
            Mtnew.Image = tnew.Image;
            if (tnew.Video != null)
            {
                string video = "http://www.youtube.com/watch?v=" + tnew.Video.ToString();
                Mtnew.Video = video;
            }
      
            return Mtnew;

        }
        public static string Edit(SiteInfoModel Mtnew)
        {
            dbVideoDataContext db = new dbVideoDataContext();

            EntitySiteInfo tnew = db.EntitySiteInfos.Where(p => p.Id == Mtnew.Id).Single();

            if (Mtnew.Video != null)
            {
                string[] arr = Mtnew.Video.ToString().Split('=');
                tnew.Video = arr[1].ToString();
            }
            string image = null;
            if (Mtnew.Image != null)
            {
                image = tnew.Image;
                tnew.Image = Mtnew.Image;
            }
            tnew.Contents = Mtnew.Contents;
            tnew.Title = Mtnew.Title;
            tnew.Date = DateTime.Now;

            db.SubmitChanges();
            return image;
        }

        public static string Delete(int Id)
        {
            dbVideoDataContext db = new dbVideoDataContext();

            EntitySiteInfo tnew = db.EntitySiteInfos.Where(p => p.Id == Id).Single();
            db.EntitySiteInfos.DeleteOnSubmit(tnew);
            db.SubmitChanges();
            return tnew.Image;
        }
    }
}